﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Core
{
    public class LoginRequiredGuard : ILoginRequiredGuard
    {
        public LoginRequiredGuard(ILoginManager loginManager, ILoginPrompt prompt)
        {
            this.Manager = loginManager;
            this.Prompt = prompt;
        }

        public void Guard(Action act)
        {
            act.ThrowIfNull("act");

            if (this.Manager.IsLoggedIn || this.Prompt.Login())
            {
                act();
            }
        }

        public void Guard(Action<UserCredentials> credentialAction)
        {
            credentialAction.ThrowIfNull("credentialAction");

            if (this.Manager.IsLoggedIn || this.Prompt.Login())
            {
                credentialAction(this.Manager.LoginCredentials);
            }
        }

        private ILoginManager Manager { get; set; }
        private ILoginPrompt Prompt { get; set; }
    }
}
